/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.web;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.UUID;

import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.xml.ws.Response;

/**
 * <Provide Type description>
 * @author amresh.singh
 */
public class WebUtils {
	
	public List<String> getErrorMessages(String errorCodeList) {
		List<String> errorMessages = new ArrayList<String>();
 		if(errorCodeList == null || "".equals(errorCodeList)) {
			return errorMessages;
		}
		StringTokenizer st = new StringTokenizer(errorCodeList, ";");
		
		ResourceBundle rb = ResourceBundle.getBundle("com.impetus.teamplan.web.messages");
		while(st.hasMoreTokens()) {
			errorMessages.add(rb.getString(st.nextToken()));
		}
		System.out.println(errorMessages);
		return errorMessages;
	}
	
	public static boolean isNullOrBlank(String s) {
		if(s == null || s.trim().equals("")) {
			return true;
		}
		return false;
		
	}
	
	public static String getMonth(int month) {
	    return new DateFormatSymbols().getMonths()[month];
	}
	
	public static String getMonth(String month) {		
	    return new DateFormatSymbols().getMonths()[Integer.parseInt(month)];
	}
	
	public static List<String> getDataFromTextArea(String text) {		
		List<String> textAreaList = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(text, "\n");
		while(st.hasMoreTokens()) {
			textAreaList.add(st.nextToken());
		}		
		return textAreaList;
	}
	
	public static String createDataForTextArea(List<String> list) {
		String output = "";
		for(String str : list) {
			output = output + str + "\n";			
		}
		return output;
	}
	
	public static String getUniqueId() {
		return UUID.randomUUID().toString();
	}
	
	public static void main(String args[]) {
		System.out.println(getMonth("00"));
	}
	
	
	public static Object getManagedBeanFromSession(String beanName) {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(beanName);
	}	
	
	public static void setManagedBeanInSession(String beanName, Object bean) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(beanName, bean);
	}
	
	public static String removeLeadingZero(String input) {
		if(input.startsWith("0")) {
			return input.substring(1);
		} else {
			return input;
		}
	}
	
	public static String addLeadingZero(String input) {
		if(input.length() == 1) {
			return "0" + input;
		} else {
			return input;
		}
	}

}
